package com.atguigu.common.config.mybatisplus.customMethod;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.scripting.defaults.RawSqlSource;

/**
 * @author 谭军(tanjun @ bosssoft.com.cn)
 * @Title: <br>
 * @Description: <br>
 * @Date 2024-06-03 15:27
 */
public class FindOneById extends AbstractMethod {
    public FindOneById() {
        this("findOneById");
    }

    public FindOneById(String methodName) {
        super(methodName);
    }


    /*@Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
//        // 自定义方法
//        // 1. 自定义sql语句
//        String customSql = String.format("SELECT %s FROM %s WHERE %s=#{%s} limit 1",
//                sqlSelectColumns(tableInfo, false),
//                tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
//        // 2. 定义在Mapper接口中方法名
//        String methodName = "findOneById";
//        // 3. 创建sql源
//        SqlSource sqlSource = languageDriver.createSqlSource(configuration,customSql,modelClass);
//        // 4. 返回方法
//        return addDeleteMappedStatement(mapperClass,methodName,sqlSource);

        *//* 执行 SQL ，动态 SQL 参考类 SqlMethod *//*
        String sql = "select *  from " + tableInfo.getTableName();
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForTable(mapperClass, sqlSource, tableInfo);
    }*/

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        SqlSource sqlSource = new RawSqlSource(configuration, String.format("SELECT %s FROM %s WHERE %s=#{%s} limit 1",
                sqlSelectColumns(tableInfo, false),
                tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty()), Object.class);
        return this.addSelectMappedStatementForTable(mapperClass, this.methodName, sqlSource, tableInfo);
    }
}
